function simulate()
{
	var trunk_id = $('#trunk_selection').val();
	var codec_field = $('#codec_selection').val();
	
    ajaxURL = AJAX_URL + '/simulate_routing/'+ trunk_id + '/' + codec_field;
            
    //alert(ajaxURL);
	$('#response').html("");
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#simulator-form').serialize(),
			async: true,
			success: function(response){

				$('#response').html(response);
			
			}
			});
}

function load_simulator_trunks(element)
{
	$('#response').html("");
	
	if($(element).val() == 'none')
	{
		$('.trunk_selection, .codec_selection, #button-simulate, .button-simulate').hide();
	}
	else
	{
		$('.codec_selection, #button-simulate, .button-simulate').hide();
	
		var carrier_id = $('#carrier_selection').val();
		ajaxURL = AJAX_URL + '/load_simulator_origin_trunks/'+ carrier_id;
            
		//alert(ajaxURL);

		$.ajax({
				type: "POST",
				url: ajaxURL,
				async: true,
				success: function(response){
					//alert(response);
					$('#trunk_selection').html(response);			
					$('.trunk_selection').show();
				}
				});				
	}
}

function load_simulator_codecs(element)
{
	$('#response').html("");
	
	if($(element).val() == 'none')
	{
		$('.codec_selection, #button-simulate, .button-simulate').hide();
	}
	else
	{
		var trunk_id = $('#trunk_selection').val();
		ajaxURL = AJAX_URL + '/load_simulator_codecs/'+ trunk_id;
            
		//alert(ajaxURL);

		$.ajax({
				type: "POST",
				url: ajaxURL,
				async: true,
				success: function(response){
					//alert(response);
					$('#codec_selection').html(response);			
					$('.codec_selection').show();
				}
				});				
	}
}

function route_origin_trunk(trunk_ID)
{
    ajaxURL = AJAX_URL + '/route_origin_trunk/'+ trunk_ID;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#route-trunk').serialize(),
			async: true,
			success: function(response){

                        reset_error_state('route_trunk');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						//document.write(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('route_trunk');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}
			}
			});
}

function load_trunks(family_id)
{
ajaxURL = AJAX_URL + '/load_family_trunks/' + family_id;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
				//alert(response);
			
				$('.family_trunks').html(response);
				create_virtual_links();
			}
		});
}


function confirm_delete_family(element, family_id, family_name)
{
	ajaxURL = AJAX_URL + '/is_family_deletion_allowed/' + family_id;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
			if(response == "true")
			{
				$('.family_name').html(family_name);

				$('#delete').dialog({
								autoOpen: true,
								width: 600,
								buttons: {
										"Yes": function() { 
										delete_family(element,family_id);
										}, 
										"No": function() { 
											$(this).dialog("close"); 
										} 
								}
				});				
			}
			else
			{
				//alert(response);
				$('.origin_trunks').html(response);

				$('#deletion_not_allowed').dialog({
								autoOpen: true,
								width: 600,
								buttons: {
										"Ok": function() { 
										$(this).dialog("close"); 
										}
								}
				});
			}
			}
		});
}

function delete_family(element, family_id)
{
	ajaxURL = AJAX_URL + '/delete_family/' + family_id;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
			if(response == "true")
			{
				$(element).parent().parent().remove();
				$('#delete').dialog('close');
			}
			else
			{
				alert(response);
			}
			}
		});
}

function create_virtual_links()
{
//alert('run');

$('.virtual-links')
	.css('text-decoration','underline')
	.mouseout(function(){
		$(this).css('cursor','default');
	})
	.mouseover(function(){
		$(this).css('cursor','pointer');
	})
	.click(function(){
		id = $(this).attr('rel');
	
		window.location.href = siteUrl + '/termination/#' + id;
	});
}


function view_family_trunks(family_id)
{
    ajaxURL = AJAX_URL + '/view_family_trunks/' + family_id;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){
			
			//alert(response);
			
				var parsed = response.split('|');
				
				$('#view_trunks').attr('title',parsed[0]);
				
				$('.trunks_list').html(parsed[1]);
							
			$('#view_trunks').dialog({
				autoOpen: true,
				width: 400,
				height: 400,
				modal: true,
				title: parsed[0],
				buttons: {
						"Ok": function() { 
							$(this).dialog("close"); 
						} 
				}
			});			
			}
			});
}


function save_new_family()
{
    ajaxURL = AJAX_URL + '/add_new_family/';
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){

						//alert(response);
			
                        reset_error_state('new_family');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_family');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}						
			}
			});

}

function save_edit_family()
{
    ajaxURL = AJAX_URL + '/edit_family/';
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){

						//alert(response);
			
                        reset_error_state('new_family');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_family');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}						
			}
			});

}

function check_family_short_name()
{
    ajaxURL = AJAX_URL + '/check_family_short_name/';
            
    //alert(ajaxURL);
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){
									
						//alert(response);
                        reset_error_state('family_short_name_check');	
						
						var parsed = response.split('|');
                        
                        var html = '';
						
						html += parsed[1];

						$( "#family_short_name_status" ). css('font-weight','bold');						
						
						if(parsed[0] == "true")
						{
							$( "#family_short_name" ). removeClass('ui-state-error');
							$( "#family_short_name_status" ). css('color','green');
							$( "#family_short_name_status" ). html(parsed[1]);
							
						}
						else if($("#family_short_name").val() != "")
						{
							$( "#family_short_name" ). addClass('ui-state-error');
							$( "#family_short_name_status" ). css('color','red');	
							$( "#family_short_name_status" ). html(parsed[1]);							
						}
						
			}
			});
}

function remove_trunk(element, trunk_id)
{
	$(element).parent().remove();
	
	$("#field_"+trunk_id).show();
	
	//alert(trunk_id);
}

// this function makes changes in database!!!
function unlink_trunk(element, trunk_id, family_id)
{
	$(element).parent().remove();
	
	$("#field_"+trunk_id).show();
	
	//alert(trunk_id);
}

function show_unlink_trunk_dialog(element, trunk_id, family_id)
{

$('#unlink-trunk').dialog({
				autoOpen: true,
				width: 600,
				buttons: {
						"Confirm": function() { 
							unlink_trunk(element,trunk_id,family_id);
							$(this).dialog("close"); 
						},
						"Cancel": function() { 
							$(this).dialog("close"); 
						}						
				}
});

}

function link_trunks()
{
    var ajaxURL = AJAX_URL + '/link_trunks/';

    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			data: $('#modal-form').serialize(),
			success: function(response){
			
				//alert(response);
				
				if(linked_trunks == 0)
				{
					$('#trunks').html(response);
					linked_trunks ++;
				}
				else
				{
					$('#trunks').append(response);
				}
			
			create_virtual_links();
			}
			});

    $('.checkboxes').each(function(){
	
		if($(this).is(':checked'))
		{
			$(this).attr('checked',false);
			$(this).parent().parent().hide();			
		}
		
	});
	$('#link-trunk').dialog( "close" );	
}

function show_hide_ips(element)
{
	if(addresses_hidden == true)
	{
		// change link text
		$(element).html("["+term_hide_inactive+"]");
		
		// show hidden rows
		$( ".hidden-ip").each(function(){

		$(this).show();	
		
		});
		addresses_hidden = false;
	}
	else
	{
		// change link text
		$(element).html("["+term_show_all+"]");
		
		// show hidden rows
		$( ".hidden-ip").each(function(){

		$(this).hide();	
		
		});
		addresses_hidden = true;
	}

}
function delete_row(element)
{
	$(element).parent().remove();
}

function disable_ip(ip_id,element)
{
	//alert(ip_id);	
    ajaxURL = AJAX_URL + '/disable_ip_address/' + ip_id + '/' + trunk_type;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
				if(response == 1)
				{
					element.dialog("close");
					$(".ip_address_"+ip_id).addClass('ui-state-disabled').addClass('hidden-ip');	
					$(".ip_address_"+ip_id).find('.button').remove();
					$(".ip_address_"+ip_id).hide();					
				}
				else
				{
					alert(response);
				}
			
			}
			});
}

function disable_ip_address_dialog(element)
{
	ip_ID = $(element).attr('rel');
	
	$('#disable-ip-address').dialog( "open" );	
	
}

function add_new_row(element)
{
	row_number = row_number + 1;

	//alert(row_number);
	
	html = '<div class="row ui-state-default ui-corner-all" id="row_'+ row_number +'">';
	
	html += '<input type="hidden" name="data[parameters]['+ row_number +']" value="'+ row_number +'"/>';
	
	html += '<input type="text" name="data[ip_address_segment_1]['+ row_number +']" class="address text ui-widget-content ui-corner-all" maxlength="3"/> . ';
	
	html += '<input type="text" name="data[ip_address_segment_2]['+ row_number +']" class="address text ui-widget-content ui-corner-all"  maxlength="3"/> . ';
	
	html += '<input type="text" name="data[ip_address_segment_3]['+ row_number +']" class="address text ui-widget-content ui-corner-all"  maxlength="3"/> . ';
	
	html += '<input type="text" name="data[ip_address_segment_4]['+ row_number +']" class="address text ui-widget-content ui-corner-all"  maxlength="3"/>';

	html += '<input type="text" name="data[tech_prefixes]['+ row_number +']" class="prefix_field text ui-widget-content ui-corner-all"/>';	
	
	html += $('.sample-row').html();
	
	html += '</div>';

	$(".rows-container").append(html);
}

function enable_trunk(trunk_ID,element)
{
    ajaxURL = AJAX_URL + '/enable_trunk/' + trunk_ID + '/' + trunk_type;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){
			
				if(response == 1)
				{
					element.dialog("close");
					$('.trunk_button_span_' + trunk_ID).html(term_enabled);
					$(".trunk_no_" + trunk_ID).removeClass('ui-state-disabled')
					$(".trunk_button_" + trunk_ID).removeClass('ui-state-highlight');
					$("#" + trunk_ID).attr('rel','1');
				}
				else
				{
					alert(response);
				}
			
			}
			});
}
function disable_trunk(trunk_ID,element)
{

    ajaxURL = AJAX_URL + '/disable_trunk/' + trunk_ID + '/' + trunk_type;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			async: true,
			success: function(response){

				if(response == 1)
				{
					element.dialog("close");
					$('.trunk_button_span_' + trunk_ID).html(term_disabled);
					$(".trunk_no_" + trunk_ID).addClass('ui-state-disabled')
					$(".trunk_button_" + trunk_ID).addClass('ui-state-highlight');
					$("#" + trunk_ID).attr('rel','0');
				}
				else
				{
					alert(response);
				}
			
			}
			});
}

function check_short_name()
{
    ajaxURL = AJAX_URL + '/check_short_name/' + trunk_type;
            
    //alert(ajaxURL);
	
	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){
									
						
                        reset_error_state('short_name_check');	
						
						var parsed = response.split('|');
                        
                        var html = '';
						
						html += parsed[1];

						$( "#short_name_status" ). css('font-weight','bold');						
						
						if(parsed[0] == "true")
						{
							$( "#short_name" ). removeClass('ui-state-error');
							$( "#short_name_status" ). css('color','green');
							$( "#short_name_status" ). html(parsed[1]);
							
						}
						else if($("#short_name").val() != "")
						{
							$( "#short_name" ). addClass('ui-state-error');
							$( "#short_name_status" ). css('color','red');	
							$( "#short_name_status" ). html(parsed[1]);							
						}
						
			}
			});
}

function activate_short_name_field()
{
	if($(".trunk").val() != undefined && $(".trunk").val() != "none")
	{
		$( "#short_name" ). removeClass('ui-state-disabled'). addClass('ui-state-active');
	}
	else
	{
		$( "#short_name" ). addClass('ui-state-disabled');	
	}
	check_short_name();
}

function save_new_trunk(trunk_type)
{
    ajaxURL = AJAX_URL + '/add_new_trunk/' + trunk_type;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#new-trunk').serialize(),
			async: true,
			success: function(response){

						//alert(response);
			
                        reset_error_state('new_trunk');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_trunk');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}
			}
			});
}


function edit_trunk(trunk_ID,type)
{
    ajaxURL = AJAX_URL + '/edit_trunk/'+ trunk_ID + '/' + type;
            
    //alert(ajaxURL);

	$.ajax({
			type: "POST",
			url: ajaxURL,
			data: $('#edit-trunk').serialize(),
			async: true,
			success: function(response){

                        reset_error_state('new_trunk');
			
                        var parsed = response.split('|');
                        
                        var html = '';
                        
						//alert(response);
						//document.write(response);
						
                        if(parsed[0] == "true")
                        {
							reset_error_state('new_trunk');						
						
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-highlight ui-corner-all')
                                        .html(html);                                																					
						}
						else
						{
						   /*
							* ***********************************************************
							* SHOW RESPONSE 
							* ***********************************************************
							*/
                             html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";                                
                             html += parsed[1] +"</p>";
							                            	
							 $('#response')	
										.addClass('ui-state-error ui-corner-all')
                                        .html(html);                                															
							
							
							
						   /*
							* ***********************************************************
							* MARK THE REQUIRED FIELDS
							* ***********************************************************
							*/
							fields = parsed[2].split('**');
						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).addClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).addClass('ui-state-error');
								}
							}
						}
			}
			});
}

function reset_error_state(form_name)
{

if(form_name == 'new_trunk')
{
	$('.row').each(function(){
	
	$(this).removeClass('ui-state-error');
	
	});

	$('#response').removeClass('ui-state-error');
	
	var fields = new Array(
							"trunk",
							"short_name",
							"trunk_balance",
							"cli",
							"codecs"
							);

						   /*
							* ***********************************************************
							* UNMARK ALL FIELDS
							* ***********************************************************
							*/						
							for(x in fields)
							{
								// mark fields by ID
								if($('#' + fields[x]).length > 0)
								{
									$('#' + fields[x]).removeClass('ui-state-error');
								}
								// mark fields by CLASS
								if($('.' + fields[x]).length > 0)
								{
									$('.' + fields[x]).removeClass('ui-state-error');
								}
							}
							
				
}

else if(form_name == 'route_trunk')
{
	$('.row').each(function(){
	
	$(this).removeClass('ui-state-error');
	
	});

	$('#response').removeClass('ui-state-error');							
	$('#family').removeClass('ui-state-error');							
				
}
}
